package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.User;
import com.itheima.reggie.service.UserService;
import com.itheima.reggie.utils.SMSUtils;
import com.itheima.reggie.utils.ValidateCodeUtils;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.server.Session;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/user")
@Api(tags = "用户相关接口")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 发送验证码
     *
     * @param user
     * @param session
     * @return
     */
    @PostMapping("/sendMsg")
    public R<String> sendMsg(@RequestBody User user, HttpSession session) {
        //获取前端发来的手机号
        String phone = user.getPhone();

        if (StringUtils.isNotEmpty(phone)) {
            //生成一个验证码
//            Integer code = ValidateCodeUtils.generateValidateCode(4);
            //为了测试方便改成固定的
            Integer code = 1234;
            log.info("验证码:{}", code);
            //用阿里云工具往这个手机号发送验证码
            //SMSUtils.sendMessage("signName","templateCode",phone,code);
            //并保存验证码  code
            session.setAttribute(phone, code);
            log.info("存到session中的验证码:" + session.getAttribute(phone).toString());
            return R.success("验证码发送成功");
        } else {
            return R.error("验证码发送失败");
        }
    }

    @PostMapping("/login")

    public R<User> login(@RequestBody Map map, HttpSession session) {
        log.info("map的数据:" + map.toString());
        //从前端获取手机号
        String phone = map.get("phone").toString();
        //从前端获取验证码
        String code = map.get("code").toString();
        //获取存储在session中的验证码
        String codeInSeesion = session.getAttribute(phone).toString();
        //比对相同则登录成功
        if (codeInSeesion != null && codeInSeesion.equals(code)) {
            //登录成功后比对手机号是否在数据库中已经存在
            QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
            userQueryWrapper.lambda().eq(User::getPhone, phone);
            User user = userService.getOne(userQueryWrapper);
            if (user == null) {
                //如果不存在就新建账号
                user = new User();
                user.setPhone(phone);
                user.setStatus(1);
                userService.save(user);
            }
            session.setAttribute("user", user.getId());
            return R.success(user);
        }

        return R.error("登录失败");
    }


    @PostMapping("/loginout")
    //退出之前删除session和线程中的东西

    public R<String> loginout( HttpSession session) {

        session.removeAttribute("user");

//        Object user = session.getAttribute("user");
//        log.info(user.toString());
        return R.success("退出成功");
    }
}